<template>
  <scroll-view
    class="top-menu-view top-bar-wrap"
    scroll-x="true"
    :scroll-into-view="tabCurrent"
  >
    <view
      class="menu-topic-view"
      v-for="(item, index) in tabs"
      :key="index"
      :id="'tabNum' + item.index"
      :class="{ active: item.selected, 'top-bar-item': true }"
      @click="onClick(item, index)"
    >
      {{ item.executionPostNameLabel }}
    </view>
  </scroll-view>
</template>
<script>
export default {
  name: 'TopBar',
  props: {
    tabBars: {
      type: Array,
      default: () => [],
      required: true,
    },
    tabIndex: {
      type: [Number, String],
      default: 0,
      required: true,
    },
    backgroundColor: {
      type: String,
      default: '',
    },
  },
  computed: {
    tabs() {
      return this.tabBars.map((item, index) => {
        item.selected = false;
        if (index === this.tabIndex) {
          item.selected = true;
        }
        return item;
      });
    },
  },
  data() {
    return { tabCurrent: 'tabNum0' };
  },
  methods: {
    onClick(obj, index) {
      this.tabCurrent = 'tabNum' + index;
      this.$emit('change', index);
    },
  },
};
</script>
<style lang="scss" scoped>
.top-bar-wrap {
  justify-content: center;
  display: flex;
  padding-top: 16px;
  .top-bar-item {
    position: relative;
    height: 38rpx;
    line-height: 38rpx;
    font-size: 36rpx;
    font-weight: 700;
    color: #999;
    text-align: center;
    margin-bottom: 32rpx;
    margin-left: 16rpx;
  }

  .active {
    color: #333;
    &::after {
      content: '';
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: -12rpx;
      height: 6rpx;
      width: 30rpx;
      background-color: #454a5e;
    }
  }
}
.top-menu-view {
  display: flex;
  left: 0;
  white-space: nowrap;
  width: 100%;
  border-top: 1rpx solid #d8dbe6;

  .menu-topic-view {
    display: inline-block;
    white-space: nowrap;
    position: relative;

    .menu-topic-text {
      font-size: 30rpx;
      color: #303133;
      padding: 10rpx 40rpx;
    }
    .menu-topic-bottom {
      position: absolute;
      bottom: 0;
      width: 100%;

      .menu-topic-bottom-color {
        width: 40rpx;
        height: 4rpx;
      }
    }

    .menu-topic-act .menu-topic-bottom {
      display: flex;
      justify-content: center;
    }

    .menu-topic-act .menu-topic-bottom-color {
      background: #3d7eff;
    }
  }
}
</style>
